package com.crocodileQ.handler.exception;

import com.crocodileQ.common.AppException;
import com.crocodileQ.common.R;
import com.crocodileQ.enums.AppExceptionMsgEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(value = BadCredentialsException.class)
    public static <T> R<T> badCredentialsException(BadCredentialsException e) throws InterruptedException {
        return R.error(AppExceptionMsgEnum.LOGIN_ERROR);
    }


    @ExceptionHandler(value = Exception.class)
    public static <T> R<T> exceptionHandle(Exception e) throws InterruptedException {
        if(e instanceof AppException){
            AppException app = (AppException) e;
            return R.error(app.getMsg(),app.getStatus());
        }

        return R.error(e.toString(),500);
    }
}
